<template>
  <div style="padding: 20px;height: 670px">
    <t-card style="height: 670px">
      <t-card shadow="never" style="width: 1320px">
        <!-- 反馈列表 -->
        <el-table :data="feedbacks" style="height: 550px">
          <el-table-column prop="fbContent" label="内容"></el-table-column>
          <el-table-column prop="fbType" label="类型"></el-table-column>
          <el-table-column prop="owner.ownerName" label="业主姓名"></el-table-column>
          <el-table-column prop="fbTime" label="时间"></el-table-column>
          <el-table-column prop="fbStatus" label="状态">
            <template slot-scope="scope">
              <template v-if="scope.row.isEditing">
                <el-select v-model="scope.row.fbStatus">
                  <el-option label="已接收" value="已接收"></el-option>
                  <el-option label="处理中" value="处理中"></el-option>
                  <el-option label="已完成" value="已完成"></el-option>
                </el-select>
              </template>
              <template v-else>
                {{ scope.row.fbStatus }}
              </template>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <t-button theme="primary" @click="editRow(scope.row)" v-if="!scope.row.isEditing">编辑</t-button>
              <t-button theme="primary" variant="dashed" @click="saveRow(scope.row)" v-else>保存</t-button>
              <el-popconfirm
                  title="确定要删除吗？"
                  confirmButtonText="确定"
                  cancelButtonText="取消"
                  @confirm="deleteFeedback(scope.row)"
                  @cancel="cancelDelete"
              >
                <t-button theme="primary" variant="outline" slot="reference">删除</t-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <br/>
        <!-- 分页 -->
        <el-pagination
            @size-change="handlePageSizeChange"
            @current-change="handlePageChange"
            :page-sizes="[5, 10, 20, 40]"
            :page-size="pageSize"
            :current-page="currentPage"
            :total=total
            layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
      </t-card>
    </t-card>

  </div>
</template>

<script>
import axios from 'axios';
import {MessagePlugin} from "tdesign-vue";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'repairs',
  data() {
    return {
      feedbacks: [], // 反馈列表
      newFeedback: {
        // 用于新增反馈的对象
        fbContent: '',
        fbType: '',
        ownerId: '',
        fbTime: '',
        fbStatus: ''
      },
      selectedFeedback: {}, // 选中的反馈对象，用于编辑和删除
      createFormVisible: false, // 新增表单是否可见
      editFormVisible: false, // 编辑表单是否可见
      pageSize: 5, // 每页显示的数量
      currentPage: 1, // 当前页码
      total: 0 // 总记录数
    };
  },
  mounted() {
    this.fetchFeedbacks(); // 页面加载时获取反馈列表
  },
  methods: {
    editRow(row) {
      // console.log(row.isEditing)
      row.isEditing = true;
    },
    saveRow(row) {
      row.isEditing = false;
      const feedback = {...row};
      this.$delete(feedback, 'isEditing');
      axios.post("/api/admin/feedback/updateFb", feedback)
          .then(res => {
                if (res.data.code == 200) {
                  MessagePlugin.success('操作成功')
                  this.fetchFeedbacks();
                }
              }
          )
    },
    cancelDelete() {
    },
    fetchFeedbacks() {
      const pageNum = this.currentPage;
      const pageSize = this.pageSize;

      axios
          .get(`/api/admin/feedback/getAllRepair?pageNum=${pageNum}&pageSize=${pageSize}`)
          .then((response) => {
            this.feedbacks = response.data.data.list.map((feedback) => ({
              ...feedback,
              isEditing: false // 添加isEditing属性，默认为false
            }));
            console.log(typeof (response.data.data.total))
            this.total = Number(response.data.data.total);
          })
          .catch((error) => {
            console.error('Error fetching feedbacks:', error);
          });
    },

    createFeedback() {
      axios
          .post('/api/feedbacks', this.newFeedback)
          .then(() => {
            this.closeForm();
            this.fetchFeedbacks();
          })
          .catch((error) => {
            console.error('Error creating feedback:', error);
          });
    },

    //   updateFeedback() {
    // const feedback={...this.selectedFeedback}
    //     axios
    //       .put(`/api/admin/feedback/updateFb`, feedback)
    //       .then((res) => {
    // 	if(res.data.code==200){
    //         this.closeForm();
    //         this.fetchFeedbacks();
    // 	}
    //       })
    //       .catch((error) => {
    //         console.error('Error updating feedback:', error);
    //       });
    //   },

    deleteFeedback(feedback) {
      axios
          .delete(`/api/admin/feedback/delete?feedbackId=${feedback.fbId}`)
          .then(res => {
            if (res.data.code == 200) {
              MessagePlugin.success('操作成功')
              this.fetchFeedbacks();
            }
          })
          .catch((error) => {
            console.error('Error deleting feedback:', error);
          });
    },

    showCreateForm() {
      this.createFormVisible = true;
    },

    showEditForm(feedback) {
      this.selectedFeedback = {...feedback};
      this.editFormVisible = true;
    },

    closeForm() {
      this.newFeedback = {
        fbContent: '',
        fbType: '',
        ownerId: '',
        fbTime: '',
        fbStatus: ''
      };
      this.selectedFeedback = {};
      this.createFormVisible = false;
      this.editFormVisible = false;
      this.$refs.feedbackForm.resetFields();
      this.$refs.editForm.resetFields();
    },

    searchFeedbacks() {
      this.currentPage = 1;
      this.fetchFeedbacks();
    },

    resetSearch() {
      this.searchContent = '';
      this.searchType = '';
      this.currentPage = 1;
      this.fetchFeedbacks();
    },

    handlePageSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.currentPage = 1;
      this.fetchFeedbacks();
    },

    handlePageChange(currentPage) {
      this.currentPage = currentPage;
      this.fetchFeedbacks();
    }
  }
};
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
}
</style>
